const express = require('express');
const multer = require('multer');
const path = require("path");
const Result = require('../models/Result');
const { UPLOAD_PATH ,UPLOAD_URL } = require('../utils/constant')
const { decoded } = require("../utils");
const  arrangeServices  = require('../services/arrange');
const Boom = require('boom');


const router = express.Router();


const storage = multer.diskStorage({
    //存储的位置
    destination(req, file, cb){
        cb(null,`${UPLOAD_PATH}/Img` )
    },
    //文件名字的确定 multer默认帮我们取一个没有扩展名的文件名，因此需要我们自己定义
    filename(req, file, cb){
        cb(null, Date.now() + file.originalname)
    }
})

const upload = multer({storage})

router.post(//上传图片
    '/upload',
    upload.single('file'),
    function(req,res){
        if(!req.file || req.file.length === 0){
            new Result(null,'上传失败').fail(res)
        }else{
            new Result({
                url:UPLOAD_URL+'/admin-upload/Img/'+req.file.filename,
                filename:req.file.filename
            },'上传成功').success(res)
        }
    }
)


router.post(//删除banner
    '/removeBanner',
    upload.single('file'),
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            arrangeServices.removeBanner(req.body).then(data=>{
                new Result(null,'删除成功').success(res)
            })
        }
    }
)


router.post(//删除banner
    '/removeCentre',
    upload.single('file'),
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            arrangeServices.removeCentre(req.body).then(data=>{
                new Result(null,'删除成功').success(res)
            })
        }
    }
)


router.post(//删除分类
    '/removeCategory',
    upload.single('file'),
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            arrangeServices.removeCategory(req.body).then(data=>{
                new Result(null,'删除成功').success(res)
            })
        }
    }
)


router.post(//查询轮播图列表
    '/bannerList',
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            arrangeServices.BannerList().then(data=>{
                new Result(data,'请求成功').success(res)
            })
        }
    }
)

function endThanTotay(endTime){
    var enddate = new Date((endTime).replace(/-/g,"/"));  
    var nowdate = new Date();
    if(nowdate > enddate)    
    {
        return 0;
    }
    else
    {
       return 1;
    }
}

router.post(//查询活动列表
    '/createList',
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            
            arrangeServices.centreList().then(data=>{
                data.forEach((element,index) => {   
                    var state = endThanTotay(element.over_time)    
                    data[index].state = state
                });
                


                new Result(data,'请求成功').success(res)
            })
        }
    }
)

router.post('/createBanner',function(req,res,next){//新增轮播图
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.insertBanner(req.body).then(()=>{
            new Result(null,'新增成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})


router.post('/createCentre',function(req,res,next){//新增活动
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.insertCentre(req.body).then(()=>{
            new Result(null,'创建活动成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})


router.post('/createCategory',function(req,res,next){//新增分类
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.insertCategory(req.body).then(()=>{
            new Result(null,'创建分类成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})





router.post('/getBanner',function(req,res){//获取轮播图详情
    const decode = decoded(req)
    if(decode && decode.username){
        arrangeServices.getBanner(req.body).then((data)=>{
            new Result(data,'查询成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})


router.post('/getCreate',function(req,res){//获取活动详情
    const decode = decoded(req)
    if(decode && decode.username){
        arrangeServices.getCreate(req.body).then((data)=>{
            new Result(data,'查询成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})

router.post('/updataBanner',function(req,res,next){//编辑轮播图
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.updataBanner(req.body).then(()=>{
            new Result(null,'编辑成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})

router.post('/updataCreate',function(req,res,next){//编辑活动
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.updataCentre(req.body).then(()=>{
            new Result(null,'更新成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})


router.post('/getCategorylist',function(req,res,next){//获取分类列表
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.getCategorylist(req.body).then((list)=>{
            new Result(list,'查询成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})


router.post('/getCategory',function(req,res){//获取分类详情
    const decode = decoded(req)
    if(decode && decode.username){
        arrangeServices.getCategory(req.body).then((data)=>{
            new Result(data,'查询成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})

router.post('/updataCategory',function(req,res,next){//编辑分类
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        arrangeServices.updataCategory(req.body).then(()=>{
            new Result(null,'更新成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})




module.exports = router